﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AzureProvider.ssdsClient;
using System.Collections.Specialized;

namespace AzureProvider.SoupMethods
{

    public class Settings
    {
        public string name;
        public string value;
    }

    public class SettingsMethods :BaseMethods 
    {
        protected override string containerId
        {
            get { return "Settings"; }
        }

        protected override string kind
        {
            get { return "Setting"; }
        }

        protected override Entity CreateEntity(object Object)
        {
            Settings myobj = (Settings)Object;

            var entity = new Entity()
            {
                Id = Guid.NewGuid().ToString(),
                Kind = kind
            };

            entity.Properties = new Dictionary<string, object>();
            entity.Properties.Add("name", myobj.name.ToString());
            entity.Properties.Add("value", myobj.value.ToString());

            return entity;
        }

        public override object CreateObject(Entity Entity)
        {
            Settings myObj = new Settings();
            myObj.name = ((string)Entity.Properties.Single(tt => tt.Key == "name").Value);
            myObj.value = ((string)Entity.Properties.Single(tt => tt.Key == "value").Value);

            return myObj;
        }


        public StringDictionary GetSettings(out string errMessage)
        {
            StringDictionary dic = new StringDictionary();

            foreach (Settings set in
                    GetObjects(null, out errMessage).Cast<Settings>())
            {
                dic.Add(set.name, set.value);
            }

            return dic;
        }

        public void SaveSettings(StringDictionary dic, out string errMessage)
        {
            
            foreach (Entity ent in GetEntities(null , out errMessage))
            {
                Delete(ent.Id);
            }

            foreach (string key in dic.Keys)                    
            {
                Insert(new Settings()
                {
                    name = key,
                    value = dic[key]
                }, out errMessage);
            }
        }
    }
}
